#include "pattern.h"
#include <getopt.h>
#include <locale.h>

//cmd line -debug open debug     
void main(int argc, char **argv)
{
 printf ("%s\n", setlocale(LC_ALL, ""));
   printf ("%ls\n",L"中\n");
 DB_BOOL  result ;
 RECORD data;
 RECORD  outdata;
 STATICS   predication[200];
 
 //STATICS*   predication;
  //predication= (STATICS*)malloc(200*sizeof(STATICS));
 
 int indicator=0;

     char **CURRENT_RECORDS;//所有记录
     int CURRENT_ROWS;
     int CURRENT_COLS;
     sqlite3*  CURRENT_DB; 
 int c=0;
      static int verbose_flag=0;
        static struct option long_options[] =
             {
               /* These options set a flag. */
               {"debug", no_argument,       &verbose_flag, 999}
             };
           /* getopt_long stores the option index here. */
           int option_index = 0;
     
           c = getopt_long_only (argc, argv, "debug",
                            long_options, &option_index);
setlocale(LC_ALL, NULL);
wchar_t *t=L"世界你好";
printf("%ls\n",t);
 printf("index %d,.....%d.....%d\n",c,option_index,verbose_flag);
 int percent=0;
 char ** name;//
 char * default1[]={D_2014029_NAME};

 if(c!=-1)// has option
 	{
 	if(verbose_flag==999)
 		 {
 		  DB_DEBUG=1;	
 		}else
 			{
 	DB_DEBUG=0;				
 				}
   }else
   	{
   		DB_DEBUG=0;
   		}
         int index0=0;   		
       if (optind < argc)
         {
         int d1=argc-optind;

          name=(char **)malloc(d1*sizeof(char*));
           printf ("non-option ARGV-elements: %d\n ",d1);
           while (optind < argc)
           {
//check pattern name is valid
            char * tvalue=argv[optind++];
            if(switchPaIndex(tvalue)==-1)
		{
				   printf ("%s not valid,skiped \n", tvalue);
			      continue;
		}
             *(name+index0)=(char*)malloc(sizeof(char*));
             name[index0]=tvalue;
             printf ("%s \n", name[index0]);
             index0++;
           }
           putchar ('\n');
         }else
         	{
             name = &default1;
         	}   	
if(index0==0)exit(0);
//testing start
uint64_t now= getCurrentTimestamp();
         			                                         
	query(CURRENT_DB,QUERY_DB,&CURRENT_RECORDS,&CURRENT_ROWS,&CURRENT_COLS);
 if(DB_DEBUG)printf("current_rows:%d,current_cols:%d\n",	CURRENT_ROWS,CURRENT_COLS);
 	
RECORD* result2 = rowsToRecords(CURRENT_RECORDS, CURRENT_ROWS, CURRENT_COLS);

if(result2!=NULL)
   {
for(int t=0;t< index0;t++)
{
indicator=0;
  printf("\rMatch Pattern:%s-------------------\n",name[t]);
   for(int i=0;i<CURRENT_ROWS;i++)
     {
     percent=i*100/CURRENT_ROWS;
     if(i%3==0) DisplayTimer(i,CURRENT_ROWS);

result=HandlePattern(result2[i],name[t],&outdata);
		 if(result==MATCH) 
		{

		  copyRECORD(&result2[i],&(predication[indicator].record));
		  copyRECORD(&outdata,&(predication[indicator].predrecord));
							  if(i<CURRENT_ROWS-1)
							     {
		copyRECORD(&result2[i+1],&(predication[indicator].nextrecord));						
						     }
		indicator++;		

		  }
     }
printSTATICS(predication,indicator);
}
free(result2);
sqlite3_free_table(CURRENT_RECORDS);
   }

uint64_t end= getCurrentTimestamp();   
uint64_t diff=end-now;
printf("%" PRIu64 " microsecond %.3f seconds consumed \n",diff,(float)diff/1000);
wchar_t *tips=L"1.形状直觉.2.关键数(规则).3.统计";
printf("%ls\n",tips);
}
